package nl.weeaboo.sh.level;

import javax.media.opengl.GL;

import nl.weeaboo.game.gl.GLDraw;
import nl.weeaboo.game.gl.GLImage;
import nl.weeaboo.game.gl.GLManager;
import nl.weeaboo.game.input.UserInput;
import nl.weeaboo.sh.danmaku.SpellUtil;
import nl.weeaboo.sh.model.TileModel;
import nl.weeaboo.sh.model.TileModel.Solidity;
import nl.weeaboo.sh.objects.Monster;

public class Tile {

	protected Level level;	
	protected Room room;
	protected TileModel model;
	protected GLImage images[];
	protected GLImage editorImages[];
	protected int layers[];
	
	private int hp;
	private int invincible;
	
	public Tile(Level level, Room room, TileModel model, int layers[]) {
		this.level = level;
		this.room = room;
		this.model = model;
		
		this.layers = layers;
		this.hp = model.getHp();
		
		images = new GLImage[layers.length];
		editorImages = new GLImage[layers.length];
	}
	
	//Functions
	public void dispose() {		
	}
	
	public void onDestroyed() {
		room.removeTile(this);
	}
	
	public void drawBackground(GLManager glm, int size) {		
		for (int n = 0; n < layers.length && layers[n] <= 0; n++) {
			if (images[n] != null) {
				images[n].draw(glm, 0, 0, size, size);
			}
		}
		
		if (invincible > 0 && invincible >= Monster.hitInvincibilityTime-5) {
			int i = invincible - Monster.hitInvincibilityTime + 5;
			float s = 2f * SpellUtil.fastSin(128 * i / Monster.hitInvincibilityTime);
			
			GL gl = glm.getGL();
			glm.pushColor();
			glm.setColor(1, 1, 1, s);
			glm.setTexture(null);
			GLDraw.fillRect(gl, 0, 0, size, size);
			glm.popColor();
		}
	}
	public void drawForeground(GLManager glm, int s) {
		int t = 0;
		while (t < layers.length && layers[t] <= 0) {
			t++;
		}
		
		while (t < layers.length) {
			if (images[t] != null) {
				images[t].draw(glm, 0, 0, s, s);
			}
			t++;
		}
	}

	public void damage(int d) {
		if (invincible == 0 && getHp() > 0) {
			hp = Math.max(0, getHp() - d);
			if (getHp() <= 0) {
				onDestroyed();
			} else {
				invincible = Monster.hitInvincibilityTime;
			}
		}
	}
	
	public void update(UserInput input, float dt) {
		if (invincible > 0) invincible--;
	}
	
	public int getHeight(int x) { return model.getHeight(x); }
	public boolean isSolidH(int dx) { return model.getSolidity() != null && model.getSolidity().isSolidH(dx); }
	public boolean isSolidV(int dy) { return model.getSolidity() != null && model.getSolidity().isSolidV(dy); }	
	
	//Getters
	public final Room getRoom() { return room; }
	public final TileModel getModel() { return model; }
	
	public Solidity getSolidity() { return model.getSolidity(); }
	public boolean isBulletReflecting() { return model.isBulletReflecting(); }
	public boolean isClimbable() { return model.isClimbable(); }
	public boolean isWallJumpable() { return model.isWallJumpable(); }
	public boolean isWater() { return model.isWater(); }
	public int getDamage() { return model.getDamage(); }
	public int getFriction() { return model.getFriction(); }
	public int getHp() { return hp; }
	
	//Setters
	public void setImage(int layer, GLImage i, GLImage ei) {
		for (int n = 0; n < layers.length; n++) {
			if (layers[n] == layer) {
				images[n] = i;
				editorImages[n] = ei;
			}
		}
		
	}
	
}
